package com.richest.api.controller.cn.client.good;

import java.util.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import com.richest.api.service.IGoodCategoryService;
import com.richest.api.service.IGoodService;
import com.richest.bean.GoodCategoryBean;
import com.richest.util.CallResults;

/**
 * 商品表 后端接口
 * 
 * @author zhaohongchao
 * @email 70375739@qq.com
 * @date 2019-03-05 08:42:07
 */
@RestController
@RequestMapping("goodController")
@Api(value = "商品管理")
@EnableAutoConfiguration
public class ApiGoodController {
	@Autowired
	private IGoodService goodService;

	@Autowired
	private IGoodCategoryService goodCategoryService;

	@RequestMapping(value = "getGoodList", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "根据商户获取商品列表（目前菜品）", value = "根据商户获取商品列表（目前菜品）", httpMethod = "GET")
	@ApiImplicitParam(name = "admart_id", value = "店铺id", required = true, paramType = "query", dataType = "Long")
	public CallResults getGoodList(@RequestParam Long admart_id) {
		// 获取商品分类
		List<GoodCategoryBean> lstGoodCategory = goodCategoryService.selectListByAdmart(admart_id);

		List<Map<String, Object>> lstReturn = new ArrayList<>();
		// 根据商品分类获取商品列表
		if (lstGoodCategory != null) {
			for (int i = 0; i < lstGoodCategory.size(); i++) {
				Map<String, Object> mapReturn = new HashMap<>();
				mapReturn.put("type", lstGoodCategory.get(i).getType());
				mapReturn.put("name", lstGoodCategory.get(i).getName());
				mapReturn.put("goodCategoryId", lstGoodCategory.get(i).getGood_category_id());

				// 根据店铺id和商品分类id获取商品列表
				Map<String, Object> params = new HashMap<>();
				params.put("admartId", admart_id);
				params.put("goodCategoryId", lstGoodCategory.get(i).getGood_category_id());
				mapReturn.put("list", goodService.selectCategoryGoodList(params));

				lstReturn.add(mapReturn);
			}
		} else {
			// 根据店铺id获取商品列表
			Map<String, Object> mapReturn = new HashMap<>();
			mapReturn.put("type", "all");
			mapReturn.put("name", "全部商品");

			// 根据店铺id和商品分类id获取商品列表
			Map<String, Object> params = new HashMap<>();
			params.put("admartId", admart_id);
			mapReturn.put("list", goodService.selectCategoryGoodList(params));

			lstReturn.add(mapReturn);
		}

		return CallResults.ok().put("data", lstReturn);
	}
}
